package com.aboo.action;

import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.ConfigService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/apollo")
public class ApolloAction {

    @Value("${spring.datasource.trade.username}")
    private String username;
    @Resource
    private JdbcTemplate tradeJdbcTemplate;
    @Resource
    private JdbcTemplate usersJdbcTemplate;

    @RequestMapping("/propFromYaml")
    public Map<String, String> propFromYaml() {
        Map<String, String> rv = new HashMap<>(8);

        Config config = ConfigService.getAppConfig();
        String attr1 = config.getProperty("spring.profiles.active", "default");
        rv.put("flag", "Y");
        rv.put("attr1", attr1);
        rv.put("username", username);

        return rv;
    }

    @RequestMapping("/propFromDb")
    public Map<String, String> propFromDb() {
        Map<String, String> rv = new HashMap<>(8);
        rv.put("flag", "Y");

        String sql = "select user()";
        String username = null;
        username = tradeJdbcTemplate.queryForObject(sql, String.class);
        rv.put("tradeJdbcTemplate", username);

        username = usersJdbcTemplate.queryForObject(sql, String.class);
        rv.put("usersJdbcTemplate", username);

        return rv;
    }

}
